package cn.itcast.crm.serviceimpl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.itcast.crm.dao.CustomerRepository;
import cn.itcast.crm.domain.Customer;
import cn.itcast.crm.service.CustomerService;

@Service
@Transactional
public class CustomerServiceimpl implements CustomerService {
	// 注入DAO
	@Autowired
	private CustomerRepository dao;



	// 保存客户信息到数据库
	@Override
	public void request(Customer customer) {
		dao.save(customer);
	}

	// 对客户和定区之间进行关联的添加和取消
	@Override
	public void assigncustomerstodecidedzoned(String customerIdStr, String fixedAreaId) {

		// 进行客户和定区的关联
		String[] integer = customerIdStr.split(",");
		for (String s : integer) {
			Integer id = Integer.parseInt(s);
			dao.updateassigncustomer(fixedAreaId, id);
		};

	}

	/*// 取消已经关联
	@Override
	public void assignNocustomerstodecidedzone(String fixedAreaId) {
		dao.updateNoassigncustomer(fixedAreaId);
	}*/

	/// 邮箱激活
	@Override
	public Customer findByTelephone(String telephone) {
		
		return dao.findByTelephone(telephone);
	}

	@Override
	public void updateTpe(String telephone) {
			dao.updateType(telephone);
	}

	//客户登录校验
	@Override
	public Customer queryLogin(String telephone, String password) {
		
		return dao.findByTelephoneAndPassword(telephone,password);
	}

	//添加根据地址  获取定区编码
	@Override
	public String findFixedAreaIdByAddress(String address) {
		return dao.findFixedAreaIdByAddress(address);
	}

	//对客户和定区关联之间进行的操作
	@Override
	public List<Customer> findNoAssociationCustomers() {
		
		return dao.findByFixedAreaIdIsNull();
	}

	@Override
	public List<Customer> findHasAssociationFixedAreaCustomers(String fixedAreaId) {
		return dao.findByFixedAreaId(fixedAreaId);
	}

	@Override
	public void assectiongetficxed(String customers, String fixedAreaId) {

		String[] custom = customers.split(",");
		for (String s : custom) {
			Integer id = Integer.parseInt(s);
			dao.updatefixedtest(fixedAreaId, id);

		}
	}
}
